package com.oschat;

import com.oschat.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLDataException;
import java.sql.SQLException;

/**
 * @Description 测试数据库连接
 * @Classname InitRun
 * @Date 2024/11/4 22:45
 * @Created by panda
 */
@Component("initRun")
@Slf4j
public class InitRun implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(InitRun.class);
    @Resource
    private DataSource dataSource;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            dataSource.getConnection();
            redisUtils.get("test");
            logger.info("数据库连接成功");
        } catch (SQLException e) {
            logger.error("数据库配置错误，请检查数据库配置");
        }catch (RedisConnectionFailureException e){
            logger.error("Redis配置错误，请检查Redis配置");
        }catch (Exception e){
            logger.error("服务启动失败",e);
        }
    }
}
